#!/usr/bin/ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'AWS/SimpleDB'
require 'basic_configuration'
require 'aws_context'
require 'pp'

# Query SimpleDB

if ARGV[0] == '-?' || ARGV.size < 1 then
  puts "usage: query domain-name [query-expression]"
  exit 1
end
domain_name = ARGV[0]
if ARGV.size > 0
  query = ARGV[1]
else
  query = nil
end

Signal.trap("INT") do
  exit 2
end

config = BasicConfiguration.new
sdb = AwsContext.instance.sdb(*config.keys)
begin
  res = sdb.query(domain_name, query, {:fetch_all => true})
  res.each { |a|  puts a }
rescue
  puts "error #{$!} #{domain_name} #{query}"
  exit 1
end

